<?php
//模板下载功能 扩展了PHPExcel第三方类库在vendor里
function downloadExc($letter,$tableheader,$data){
    //include './phpexcel/PHPExcel.php';
    //创建对象
    //import("Home.Util.PHPExcel");
    import('Vendor.PHPExcel.PHPExcel','','.php');//导入类库
    $excel = new \PHPExcel();//类库没有用命名空间时 加 \  否则会调用当前命名空间
   

    //填充表头信息
    for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
    }

    //填充表格信息
    for ($i = 2;$i <= count($data) + 1;$i++) {
        $j = 0;
        foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
            $j++;
        }
    }

    $write = new \PHPExcel_Writer_Excel5($excel);
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    header("Content-Type:application/force-download");
    header("Content-Type:application/vnd.ms-execl");
    header("Content-Type:application/octet-stream");
    header("Content-Type:application/download");;
    header('Content-Disposition:attachment;filename="testdata.xls"');
    header("Content-Transfer-Encoding:binary");
    $write->save('php://output');
}


function upload($exts,$rootPath,$savePath,$msg){

    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     $exts;// 设置附件上传类型
    $upload->rootPath  =      $rootPath; // 设置附件上传根目录
    $upload->savePath  =      $savePath; // 设置附件上传（子）目录
    // 上传文件

    $info   =   $upload->upload();
    if(!$info) {// 上传错误提示错误信息
        $data['success'] = false;
        $data['msg'] = $upload->getError();
        //$this->error($upload->getError());
        return  $data;
    }else{// 上传成功 获取上传文件信息

        foreach($info as $file){
            // echo  $file['savepath'].$file['savename'];
            $data['success'] = true;
            $data['msg'] = $msg;
            $data['file'] =   $file['savepath'].$file['savename'];

            //$this->ajaxReturn($data);
            return $data;
        }
    }
}